package com.yg.controller;


import com.yg.config.result.R;
import com.yg.entity.readAndWriteTemplates.ResearchProjectRead;
import com.yg.entity.singleEntity.ResearchProject;
import com.yg.service.ResearchProjectService;
import com.yg.utils.CloseStream;
import com.yg.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 对应sheet05，此表是教研项目表 前端控制器
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */
@RestController
@RequestMapping("/researchProject")
@CrossOrigin
public class ResearchProjectController {

    @Autowired
    ResearchProjectService researchProjectService;

    /**
     * 查询所有
     * clr
     *
     * @return
     */
    @GetMapping("/selectAllResearchProjectInfo")
    public R selectAllResearchProjectInfo(@RequestParam("current") int current, @RequestParam("limit") int limit) {
        R r = researchProjectService.selectAllResearchProjectInfo(current, limit);
        return r;
    }


    /**
     * 读取excel写入数据库
     * clr
     *
     * @param file
     * @return
     */
    @PostMapping("/readExcel")
    public R readExcel(HttpServletRequest httpServletRequest,MultipartFile file) {
        //获取fileInputStream
        FileInputStream fileInputStream = null;
        //判断权限
        String vipByJwtToken = JwtUtils.getVipByJwtToken(httpServletRequest);
        if ("v2".equals(vipByJwtToken)) {
            return R.ERROR().message("警告：请以管理员身份登录");
        }
        R r = null;
        try {
            fileInputStream = (FileInputStream) file.getInputStream();
            r = researchProjectService.readExcel(fileInputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭fileInputStream
            CloseStream.closeFileInputStream(fileInputStream);
        }
        return r;
    }


    /**
     * 导出excel
     * clr
     *
     * @return
     */
    @GetMapping("/writeExcel")
    public R writeExcel(HttpServletResponse response, @RequestParam(value = "name",required = false)String name,@RequestParam(value = "type",required = false)String type) throws UnsupportedEncodingException {
        ResearchProjectRead researchProjectRead = new ResearchProjectRead();
        researchProjectRead.setName(name);
        researchProjectRead.setType(type);
        R r = researchProjectService.writeExcel(response, researchProjectRead);
        return r;
    }


    /**
     * 条件查询
     * clr
     *
     * @return
     */
    @PostMapping("/researchProjectQuery")
    public R researchProjectQuery(@RequestBody String researchProjectCondition, @RequestParam("current") int current, @RequestParam("limit") int limit) {
        R r = researchProjectService.researchProjectConditionalQuery(researchProjectCondition, current, limit);
        return r;
    }

    /**
     * 根据id更新教师信息
     *
     * @return
     */
    @PutMapping("/updateResearchProjectInfo")
    public R updateResearchProjectInfo(@RequestBody String researchProjectCondition) {
        R r = researchProjectService.updateResearchProjectInfo(researchProjectCondition);
        return r;
    }

    /**
     * 根据id删除教师信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteResearchProjectById")
    public R deleteResearchProjectById(@RequestBody String id) {
        R r = researchProjectService.deleteResearchProjectById(id);
        return r;
    }

    /**
     * 添加一个教研项目
     *
     * @param researchProject
     * @return
     */
    @PostMapping("/insertTeacherPractice")
    public R insertResearchProject(@RequestBody ResearchProject researchProject) {
        R r = researchProjectService.insertResearchProjectInfo(researchProject);
        return r;
    }
}
